package com.shiwaixiangcun.customer.ui.activity;

import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.baidu.mobstat.SendStrategyEnum;
import com.baidu.mobstat.StatService;
import com.shiwaixiangcun.customer.GlobalApi;
import com.shiwaixiangcun.customer.R;
import com.shiwaixiangcun.customer.entity.ResponseEntity;
import com.shiwaixiangcun.customer.ui.IDetailView;
import com.shiwaixiangcun.customer.ui.dialog.ShareDialog;
import com.shiwaixiangcun.customer.utils.SdCordUtil;
import com.shiwaixiangcun.customer.utils.Utils;
import com.shiwaixiangcun.customer.widget.ChangeLightImageView;
import com.shiwaixiangcun.customer.widget.X5WebView;
import com.tencent.smtt.sdk.WebView;
import com.tencent.smtt.sdk.WebViewClient;

/**
 * @author Administrator
 */
public class ViewMoreActivity extends AppCompatActivity implements View.OnClickListener,IDetailView{

    private ChangeLightImageView back_left;
    private ImageView iv_share_right;
    private X5WebView webview;
    private String articleId;
    private String str_web = GlobalApi.INSTANCE.getDOMAIN_PM() + "/mi/merchant/detail/view.htm";
    private String detailtitle;
    private TextView tv_page_name;
    private View tv_top;
    private String detailcontent;
    private String shareimage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_details);

        Resources res = getResources();

        Bitmap bmp= BitmapFactory.decodeResource(res, R.drawable.start_page);
        shareimage = SdCordUtil.saveMyBitmap("shareimage", bmp);
        //        百度统计
        StatService.setLogSenderDelayed(10);
        StatService.setSendLogStrategy(this, SendStrategyEnum.APP_START, 1, false);
        StatService.setSessionTimeOut(30);

        Intent intent = getIntent();
        articleId = intent.getStringExtra("articleId");
        detailtitle = intent.getStringExtra("detailtitle");
        detailcontent = intent.getStringExtra("detailcontent");

        layoutView();
        initData();

        webview = findViewById(R.id.webview);
        //设置WebView属性，能够执行Javascript脚本
        webview.getSettings().setJavaScriptEnabled(true);
        //加载需要显示的网页
        webview.loadUrl(str_web);
        //设置Web视图
        webview.setWebViewClient(new HelloWebViewClient());

        webview.setOnScrollChangeListener(new X5WebView.OnScrollChangeListener() {
            @Override
            public void onPageEnd(int l, int t, int oldl, int oldt) {

            }

            @Override
            public void onPageTop(int l, int t, int oldl, int oldt) {

            }

            @Override
            public void onScrollChanged(int l, int t, int oldl, int oldt) {

            }

            @Override
            public void scrollDistance(float distant) {
                if (distant > 300) {
                    if (Utils.isNotEmpty(detailtitle)){
                        tv_top.setVisibility(View.VISIBLE);
                    }

                }else {
                    tv_top.setVisibility(View.GONE);
                }
            }
        });



    }

    private void layoutView() {
        back_left = findViewById(R.id.back_left);
        iv_share_right = findViewById(R.id.iv_share_right);
        tv_top = findViewById(R.id.tv_top);
        tv_page_name = findViewById(R.id.tv_page_name);
        tv_page_name.setEllipsize(TextUtils.TruncateAt.END);
        tv_page_name.setLines(1);
        tv_page_name.setText("商家介绍");
    }

    private void initData() {
        str_web += "?id=" + articleId;

        iv_share_right.setVisibility(View.GONE);
        back_left.setOnClickListener(this);
        iv_share_right.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        switch (id){
            case R.id.back_left:
                finish();
                break;
            case R.id.iv_share_right:
                showShare();
                break;
            default:
                break;
        }
    }


    @Override
    public void setBgaAdpaterAndClickResult(ResponseEntity result) {

    }

    private void showShare() {


        ShareDialog shareDialog = new ShareDialog();
        Bundle bundle = new Bundle();
        bundle.putString("title", detailtitle);
        bundle.putString("titleUrl", str_web);
        bundle.putString("text", detailcontent);
        bundle.putString("imageUrl", "http://resource.hxteb.com/group1/M00/00/26/rBKx5Vl4TMCAUPgUAAB6YxNdWvs030.png");
        bundle.putString("siteUrl", str_web);
        shareDialog.setArguments(bundle);
        shareDialog.show(this.getFragmentManager(), "shareDialog");


    }

    @Override
    protected void onResume() {
        super.onResume();
        StatService.onResume(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        StatService.onPause(this);
    }

    //Web视图
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}
